package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.form.DesignFormBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.dto.DrFollowUpRecordDTO;
import com.naiterui.ehp.bs.doctor.service.IDrFollowUpRecordService;
import com.naiterui.ehp.bs.doctor.vo.followup.AdCalendarPointVO;
import com.naiterui.ehp.bs.doctor.vo.followup.AdVisitSettingVO;
import com.naiterui.ehp.bs.doctor.vo.followup.AdVisitVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpFormTypeVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpFormVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpRecordFormVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpRecordVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpSubVisitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 随访任务
 *
 * @author yangfuya
 * @since 2022/1/7
 */
@Slf4j
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.FOLLOW_UP})
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("ad/followup")
public class AdFollowUpRecordController {
    private final IDrFollowUpRecordService followUpRecordService;

    @GetMapping
    @ApiOperation(value = "随访管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生Id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 0.未开始 1.进行中 2.已结束 ", required = true, dataType = "int",
                    paramType = "query"),
            @ApiImplicitParam(name = "inquirerName", value = "就诊人姓名", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序 asc.升序 desc.降序", dataType = "string", paramType = "query")})
    public PageVO<DrFollowUpRecordVO> page(@NotNull(message = "医生id不能为空") @RequestParam Long doctorId,
                                           @NotNull(message = "状态不能为空") @RequestParam Integer status, String inquirerName, String sort,
                                           PageParamVO pageParamVO)
            throws BusinessException {
        return this.followUpRecordService.page(doctorId, status, inquirerName, sort, pageParamVO);
    }

    @GetMapping("/form/type")
    @ApiOperation(value = "获取随访表单类型")
    public List<DrFollowUpFormTypeVO> getFollowUpFormTypeList() throws BusinessException {
        return this.followUpRecordService.getFollowUpFormTypeList();
    }

    @GetMapping("/form")
    @ApiOperation(value = "分页获取随访表单集合列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生Id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "formName", value = "问卷名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "随访表单类型（按名称搜索时type传0）", required = true, dataType = "int", paramType = "query")
    })
    public PageVO<DrFollowUpFormVO> followUpFormPage(@NotNull(message = "医生id不能为空") @RequestParam Long doctorId,
                                                     @NotNull(message = "类型不能为空") @RequestParam Integer type, String formName, PageParamVO pageParamVO) throws BusinessException {
        return this.followUpRecordService.followUpFormPage(doctorId, type, formName, pageParamVO);
    }

    @GetMapping("/form/view")
    @ApiOperation(value = "预览form表单")
    public DesignFormBO viewForm(@RequestParam Long followUpFormId) throws BusinessException {
        return this.followUpRecordService.viewForm(followUpFormId);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "医生端获取单个随访详情（包含复诊和问卷详情）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "随访计划id", required = true, dataType = "long", paramType = "query")
    })
    public DrFollowUpRecordVO getQaDetailById(@RequestParam Long id) throws BusinessException {
        return this.followUpRecordService.getById(id);
    }

    @GetMapping("/subsequent/visit/detail")
    @ApiOperation(value = "医生端获取包含多次复诊的计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "followNo", value = "随访复诊No", required = true, dataType = "String", paramType = "query")
    })
    public List<DrFollowUpSubVisitVO> getById(@RequestParam String followNo) throws BusinessException {
        return this.followUpRecordService.getSubsequentVisitByFollowNo(followNo);
    }

    @GetMapping("/switchForm")
    @ApiOperation(value = "根据随访表单id切换表单")
    @ApiImplicitParam(name = "followUpRecordFormId", value = "随访任务表单id", required = true, dataType = "long",
            paramType = "query")
    public DrFollowUpRecordFormVO switchForm(@RequestParam Long followUpRecordFormId) throws BusinessException {
        return this.followUpRecordService.switchForm(followUpRecordFormId);
    }

    @PostMapping
    @ApiOperation(value = "新增随访计划")
    public void save(@Valid @RequestBody DrFollowUpRecordDTO drFollowUpRecordDTO) throws BusinessException {
        this.followUpRecordService.save(drFollowUpRecordDTO);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除随访计划")
    public void delete(@RequestParam Long id) throws BusinessException {
        this.followUpRecordService.delete(id);
    }

    @PostMapping("setting")
    @ApiOperation(value = "修改随访为报道，取消", notes = "修改随访为报道，取消")
    public void setVisitRecordStatus(@Valid AdVisitSettingVO settingVO) throws BusinessException {
        log.info("医生修改随访为报道，取消 请求参数：AdVisitSettingVO = {}", settingVO);
        this.followUpRecordService.updateVisitRecordStatus(settingVO);
    }

    @GetMapping("today")
    @ApiOperation(value = "医生某天的随访计划", notes = "医生某天的随访计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "具体日期 yyyy-MM-dd ", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")
    })
    public PageVO<AdVisitVO> todayList(@NotNull(message = "医生ID不能为null") Long doctorId, String date, PageParamVO pageParamVO) throws BusinessException {
        log.info("医生某天的随访计划 请求参数：doctorId = {}, date = {}, pageParamVO = {} ", doctorId, date, pageParamVO);
        return this.followUpRecordService.getVisitTodayPage(doctorId, date, pageParamVO);
    }

    @GetMapping("month")
    @ApiOperation(value = "医生某月的随访计划日期展示", notes = "医生某月的随访计划日期展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份 yyyy-MM ", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")
    })
    public List<AdCalendarPointVO> getMonthVisit(@NotNull(message = "医生ID不能为null") Long doctorId,
                                                 @NotEmpty(message = "月份不能为null") String month) throws BusinessException {
        log.info("医生某月的随访计划日期展示 请求参数：doctorId = {}, month = {}", doctorId, month);
        return this.followUpRecordService.getMonthVisit(doctorId, month);
    }

    @GetMapping("option")
    @ApiOperation(value = "获取添加随访选项", notes = "获取添加随访选项")
    public List<Map<String, Object>> getOption() {
        return this.followUpRecordService.getOption();
    }

}
